#pragma once
#include <iostream>
#include <pthread.h>
#include <unistd.h>

namespace ns_task
{
    class task
    {
    public:
        task()
        {
        }

        task(int sock)
            : _sock(sock)
        {
        }
        void run()
        {
            while (1)
            {
            #define NUM 1024
                char buf[NUM] = {0};
                ssize_t sz = read(_sock, buf, NUM - 1);
                if (sz > 0)
                {
                    buf[sz] = 0;
                    std::cout << pthread_self() << "# " <<  buf << std::endl;

                    std::string s;

                    s += ">>server<< ";
                    s += buf;

                    write(_sock, s.c_str(), s.size());
                }
                else if (sz == 0)
                {
                    std::cout << "对端关闭" << std::endl;
                    return;
                }
                else
                {
                    return;
                }
            }
        }

        void operator()()
        {
            run();
        }

    private:
        int _sock;
    };
}